<?php
/**
 * Created by PhpStorm.
 * User: Jason.Jiang
 * Date: 2015/9/18
 * Time: 11:42
 */

class View
{

    public $view_name = '';
    public $view_upper = '';
    public $view_folder = '';

    private $view_arr = array('index', 'add', 'update');

    public function __construct($view)
    {
        $this->view_name = strtolower($view);
        $this->view_upper = ucfirst($this->view_name);
        $this->view_folder = PROJECT_PATH . DS . 'application' . DS . 'views';

        $this->run();
    }

    public function run()
    {
        $view_model_path = $this->view_folder . DS . $this->view_upper;

        if(!is_writable($view_model_path))
        {
            @mkdir($view_model_path, 755);
        }

        foreach($this->view_arr as $row)
        {
            $template_path = MODULE . DS . 'views' . DS . $row .'.template.php';
            $view_save_path = $view_model_path . DS . $row . '.php';
            $view_template = @file_get_contents($template_path);
            $view_con = str_replace('{$model_name}', $this->view_name, $view_template);
            @file_put_contents($view_save_path, $view_con);
        }
    }
}